<script>
import { mapGetters, mapActions } from "vuex";
import Recommend from "../../components/recommend.vue";
import { statcUrl } from "../../api/api";
import { updateProfile } from "../../api/otherServer";
import address from "../address/address.vue";
import { Notify } from "vant";
import confirmOrder from "../../components/confirmOrder.vue";
export default {
  data() {
    return {
      showLeft: this.$route.query.id, //获取当前的参数
      statcUrl,
      value: 0,
      checked: [],
      checkedAll: false,
      show: "car",
      selectAdress: "",
      // checkboxGroup,
    };
  },
  components: {
    recommend: Recommend,
    addressCom: address,
    comfiremorder: confirmOrder,
  },
  computed: {
    ...mapGetters("usermessage", [
      "shoppNum",
      "shoppData",
      "shoppNum",
      "getProfile",
    ]),
    //需要购买商品的总价
    totallPrice() {
      let count = 0;
      this.shoppData.forEach((item) => {
        if (
          this.checked.includes(item.id + "-" + item.size + "-" + item.color)
        ) {
          count += item.price * item.num;
        }
      });
      return count * 100;
    },
    showaddress() {
      return this.selectAdress
        ? this.selectAdress.name + "," + this.selectAdress.tel
        : "请您填写地址";
    },
    //计算需要购买商品的数据
    selectPro() {
      return this.shoppData.filter((item) => {
        return this.checked.includes(
          item.id + "-" + item.size + "-" + item.color
        );
      });
    },
  },
  methods: {
    ...mapActions("usermessage", ["setprofileActions"]),
    handelImg() {
      //商品checkbox的选择
      if (this.checked.length > 0 && this.checked.length == this.shoppNum) {
        this.checkedAll = true;
      } else {
        this.checkedAll = false;
      }
      console.log(this.checked);
    },

    onSubmit() {
      console.log(this.selectPro, this.checked);
      if (this.checked.length === 0 || !this.checked) {
        Notify("请您至少添加一款商品才能购买");
        return;
      }
      if (!this.selectAdress) {
        Notify("请添加收获地址");
        return;
      }
      this.show = "comfireorder";
    },
    handelCheckall() {
      this.$refs.checkboxGroup.toggleAll(this.checkedAll);
    },
    handelDel() {
      for (let i = 0; i < this.shoppData.length; i++) {
        if (
          this.checked.includes(
            this.shoppData[i].id +
              "-" +
              this.shoppData[i].size +
              "-" +
              this.shoppData[i].color
          )
        ) {
          this.shoppData.splice(i, 1);
          i--;
        }
      }
      //判断checked里面的数据还是否有如果常度等于0的话那么就证明商品都被删除了，不是全选状态
      if (this.shoppData.length == 0) {
        this.checked = false;
      }
      // 把更新的数据更新到后端和store状态
      updateProfile(
        {
          filedname: "shoppingcart",
          filedvalue: JSON.stringify(this.shoppData),
        },
        () => {
          //更新到store状态
          this.setprofileActions({
            data: {
              ...this.getProfile,
              shoppingcart: JSON.stringify(this.shoppData),
            },
          });
        }
      );
    },
    //减小商品的数量
    minusNum(index) {
      if (this.shoppData[index].num > 1) {
        this.shoppData[index].num--;
      }
      updateProfile(
        {
          filedname: "shoppingcart",
          filedvalue: JSON.stringify(this.shoppData),
        },
        () => {
          //更新到store状态
          this.setprofileActions({
            data: {
              ...this.getProfile,
              shoppingcart: JSON.stringify(this.shoppData),
            },
          });
        }
      );
    },
    //增大商品的数量
    plusNum(index) {
      this.shoppData[index].num++;
      updateProfile(
        {
          filedname: "shoppingcart",
          filedvalue: JSON.stringify(this.shoppData),
        },
        () => {
          //更新到store状态
          this.setprofileActions({
            data: {
              ...this.getProfile,
              shoppingcart: JSON.stringify(this.shoppData),
            },
          });
        }
      );
    },
    onclickLink(type) {
      this.show = type;
    },
    hideAdress(type) {
      //隐藏地址组件
      this.show = type;
    },
    //隐藏订单页面的函数
    hideCom(type) {
      this.show = type;
    },
    showSelectaddress(selectAdresse) {
      this.selectAdress = selectAdresse;
    },
  },
};
</script>
<template>
  <!-- 复选框组组件 -->
  <div v-show="show == 'car'">
    <van-nav-bar
      :title="`购物车(${shoppNum})`"
      :left-arrow="showLeft == 'detail'"
      @click-left="$router.go(-1)"
    />
    <van-checkbox-group
      v-model="checked"
      @change="handelImg"
      ref="checkboxGroup"
    >
      <div class="shopp_container">
        <div
          class="shopp"
          v-for="(item, index) in shoppData"
          :key="item.id + index"
        >
          <div class="checkBox">
            <van-checkbox
              :name="item.id + '-' + item.size + '-' + item.color"
            ></van-checkbox>
          </div>

          <div class="checkBox_1">
            <van-card
              :num="item.num"
              :price="item.price"
              :title="item.title"
              :thumb="statcUrl + '/' + item.img"
            >
              <template #tags>
                <van-tag plain type="danger">{{ item.color }}</van-tag>
                <van-tag plain type="danger">{{ item.size }}</van-tag>
              </template>
              <template #footer>
                <van-button size="mini"
                  ><van-icon name="plus" @click="plusNum(index)"
                /></van-button>
                <van-button size="mini"
                  ><van-icon name="minus" @click="minusNum(index)"
                /></van-button>
              </template>
            </van-card>
          </div>
        </div>
      </div>
    </van-checkbox-group>
    <!-- 提交定单 -->
    <van-submit-bar
      :price="totallPrice"
      button-text="提交订单"
      @submit="onSubmit"
    >
      <van-checkbox @click="handelCheckall" v-model="checkedAll"
        >全选</van-checkbox
      >
      <van-button type="primary" @click="handelDel">删除</van-button>
      <template #tip>
        {{ showaddress }}
        <!--点击添加收获地址跳到收货地址那 -->
        <span>
          <van-button type="default" @click="onclickLink('address')"
            >添加收货地址</van-button
          ></span
        >
      </template>
    </van-submit-bar>
    <recommend />
  </div>
  <!-- 显示地址组件 -->
  <div v-show="show == 'address'">
    <addressCom
      :hideAdress="hideAdress"
      :showSelectaddress="showSelectaddress"
    ></addressCom>
  </div>
  <!-- 显示订单组件 -->
  <div v-show="show == 'comfireorder'">
    <comfiremorder
      :hideCom="hideCom"
      :totallPrice="totallPrice"
      :selectPro="selectPro"
      :showaddress="showaddress"
      :checked="checked"
    ></comfiremorder>
  </div>
</template>

<style src="./index.scss"></style>
